<?php

namespace App\Admin\Controllers\Article;

use App\Http\Controllers\Controller;
use App\Models\Article\Column;
use App\Models\Article\Post;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;

class PostController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('文章');
            $content->description('管理');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑');
            $content->description('文章');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('添加');
            $content->description('文章');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Post::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->columnInfo()->name("栏目");
            $grid->column('title', '标题');
            $grid->column('author', '作者');
            $grid->column('source', '来源');
            $grid->comments('评论数')->display(function ($comments) {
                $count = count($comments);
                return "<span class='label label-warning'>{$count}</span>";
            });
            $grid->created_at('添加时间');
            $grid->updated_at('更改时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Post::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->text('title', '文章标题');

            $form->select('c_id', '栏目')->options(Column::all()->pluck('name', 'id'))->setWidth(2)->help("选择栏目");

            $form->textarea('describe', '文章描述');
            $form->multipleImage('thumb', '缩略图')->help('可为空，最多上传三张，可上传1张 3张');
            //$form->text('thumb', '缩略图')->help('可为空，最多上传三张，可上传1张 3张');
            $form->text('author', '作者')->setWidth(2)->setDefault(Admin::user()->name);
            $form->text('source', '来源')->setWidth(2)->setDefault("张敏添加");
            $form->editor('content', '详情内容')->attribute(["style" => "height:400px;"]);
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }

    public function users(Request $request)
    {
        $q = $request->get('q');

        return Post::where('name', 'like', "%$q%")->paginate(null, ['id', 'title as text']);
    }
}
